<template>
	<el-dialog :title="form.id ? '编辑' : '新增'" :visible.sync="visible" :close-on-click-modal="false">
		<el-form ref="form" :model="form" :rules="rules">
			<el-form-item label="收货人" :label-width="formLabelWidth" prop="consignee">
				<el-input v-model="form.consignee" autocomplete="off" placeholder="请输入收货人"></el-input>
			</el-form-item>
			<el-form-item label="手机号" :label-width="formLabelWidth" prop="phone">
				<el-input v-model="form.phone" autocomplete="off" maxlength="11" placeholder="请输入手机号"></el-input>
			</el-form-item>
			<el-form-item label="省市区" :label-width="formLabelWidth" prop="province">
				<fortress-region ref="fortressRegion" clearable placeholder="请选择地区" @ftregion="regionHandler"></fortress-region>
			</el-form-item>
			<el-form-item label="详细地址" :label-width="formLabelWidth" prop="address">
				<el-input v-model="form.address" autocomplete="off" placeholder="请输入详细地址"></el-input>
			</el-form-item>
			<el-form-item label="默认地址" :label-width="formLabelWidth" prop="isDefault">
				<el-radio-group v-model="form.isDefault">
					<el-radio label="1">是</el-radio>
					<el-radio label="2">否</el-radio>
				</el-radio-group>
			</el-form-item>
		</el-form>
		<div slot="footer" class="dialog-footer">
			<el-button @click="visible = false" size="medium" type="danger">取 消</el-button>
			<el-button type="primary" size="medium" @click="submitForm('form')">确 定</el-button>
		</div>
	</el-dialog>
</template>
<script>
import { memberaddressInfo, memberaddressSave, memberaddressEdit } from "@/api/member/memberaddress.js"
import { enumList } from "@/api/public.api.js"

export default {
	props: {
		id: {
			type: [String, Number],
			default: "",
		},
	},
	data() {
		return {
			visible: false,
			form: {
				memberId: "",
				consignee: "",
				phone: "",
				province: "",
				city: "",
				area: "",
				address: "",
				isDefault: "2",
			},
			formLabelWidth: "120px",
			rules: {},
			YesNoList: [],
		}
	},
	created() {},
	methods: {
		queryEnumList() {
			this.YesNoList = []
			enumList("YesNo", "").then(resp => {
				if (resp.code == 0) {
					this.YesNoList = resp.data
				}
			})
		},
		init(id) {
			this.resetForm()
			this.queryEnumList()
			//修改
			if (id) {
				memberaddressInfo(id).then(resp => {
					if (resp.code == 0) {
						this.$nextTick(() => {
							this.form = {
								...this.form,
								...resp.data,
								isDefault: resp.data.isDefault.toString(),
							}
							this.$refs.fortressRegion.init([resp.data.provinceCode, resp.data.cityCode, resp.data.areaCode])
						})
					} else {
						this.$msg({
							message: resp.msg,
							type: "error",
						})
					}
				})
			}
			this.visible = true
		},
		regionHandler(_r, regionsInfo) {
			const [province, city, area] = regionsInfo
			this.form.province = province.name
			this.form.provinceCode = province.id
			this.form.city = city.name
			this.form.cityCode = city.id
			this.form.area = area.name
			this.form.areaCode = area.id
		},
		//提交表单数据
		submitForm() {
			this.$refs["form"].validate(valid => {
				if (valid) {
					//修改
					if (this.form.id) {
						memberaddressEdit(this.form).then(resp => {
							if (resp.code == 0) {
								this.$message({
									type: "success",
									message: "操作成功!",
								})
								this.$emit("refreshData")
								this.visible = false
							} else {
								this.$msg({
									message: resp.msg,
									type: "error",
								})
							}
						})
						//新增
					} else {
						this.form.memberId = this.id
						memberaddressSave(this.form).then(resp => {
							if (resp.code == 0) {
								this.$message({
									type: "success",
									message: "操作成功!",
								})
								this.$emit("refreshData")
								this.visible = false
							} else {
								this.$msg({
									message: resp.msg,
									type: "error",
								})
							}
						})
					}
				} else {
					console.log("error submit!!")
					return false
				}
			})
		},
		resetForm() {
			this.form = {
				memberId: "",
				consignee: "",
				phone: "",
				province: "",
				city: "",
				area: "",
				address: "",
				isDefault: "2",
			}
			this.$nextTick(() => {
				if (this.$refs["form"]) {
					this.$refs["form"].resetFields()
				}
			})
		},
	},
}
</script>
<style scoped>
.avatar-uploader .el-upload {
	border: 1px dashed #d9d9d9;
	border-radius: 6px;
	cursor: pointer;
	position: relative;
	overflow: hidden;
}
.avatar-uploader .el-upload:hover {
	border-color: #409eff;
}
.avatar-uploader-icon {
	font-size: 28px;
	color: #8c939d;
	width: 178px;
	height: 178px;
	line-height: 178px;
	text-align: center;
}
.avatar {
	width: 178px;
	height: 178px;
	display: block;
}
.el-scrollbar .el-scrollbar__view .el-select-dropdown__item {
	height: auto;
	max-height: 274px;
	padding: 7px;
	overflow: hidden;
	overflow-y: auto;
	font-weight: normal;
}
</style>
